import {sendSuccess, sendError} from "../../utils/responseHandler";
import {User} from "../../../db/models/user";
import {Work} from "../../../db/models/work";
import {WorkSeeker} from "../../../db/models/work_seekers";
//应聘
export default async function handler(req, res) {
    if (req.method === 'PUT') {
        try {
            const { userId,workId, status} = req.body;

            //查一下是否有这个用户
            const userInfo = await User.findByPk(userId);

            if (!userInfo) {
                sendError(res, '用户不存在!', 401)
                return;
            }

            const seekerInfo = await WorkSeeker.findOne({
                where:{
                    user_id:userId,
                    work_id:workId
                }
            })

            console.log('122112',seekerInfo)

            if (!seekerInfo) {
                sendError(res, '数据不存在！', 401)
                return;
            }

            await seekerInfo.update({
                status: status
            }, {where: {work_id: workId,user_id:userId}})
            sendSuccess(res, '1');
        } catch (error) {
            console.error('创建失败:', error);
            sendError(res, '创建失败', 500);
        }
    } else {
        res.setHeader('Allow', ['GET', 'POST']);
        res.status(405).end(`Method ${req.method} Not Allowed`);
    }
}
